<?php


class Crawl_sina_info_from_phone_library  extends Crawl_keyword_library{

    private $_header;
    private $_userAgent;
    private $_url;
    private $_cookie;
    private $_parameters = '';
    public  $_htmlsearch_config;

    /**
     * 定义 CURL 消息头、用户端信息、抓取地址
     * 定义 新浪网页抓取选项
     */
    function __construct(){
        parent::__construct();
        $openplatforms = $this->_ci->config->item('openplatforms');
        $this->_htmlsearch_config  = $openplatforms['openplatforms']['htmlsearch']['sina_phone'];
        $this->_null_tab    = $this->_htmlsearch_config['null'];

        $this->_header      = $this->_htmlsearch_config['header'];
        $this->_userAgent   = $this->_htmlsearch_config['user_agent'];
        $this->_url         = $this->_htmlsearch_config['url'];

        $this->write_log_stary_tag();
    }


    public function crawl($retry = FALSE, $preg_url) {
        $crawl_result = $this->try_crawl($retry, $preg_url);

        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        } else if(is_array($crawl_result) && array_key_exists('data', $crawl_result) && is_null($crawl_result['data'])) {
            return $crawl_result;
        } else {
            return $this->make_data($crawl_result);
        }
    }

    private function make_data($result = NULL) {
		
    	//return ;
		
        /*$this->_ci->load->model('split/htmlsearchparser_model');
        $split_data = $this->_ci->htmlsearchparser_model->parser_sina_phone($this->_htmlsearch_config, $result);
        
        if($split_data == FALSE) {
            $error = $this->_ci->error_code_library->getError('make_data', 'data', 'match_split_data');
            log_message(self::LOG_LEVEL_WARING, json_encode($error), FALSE, $this->_log_dir);
            return $error;
        }*/
    	
//     	$data = json_decode($result, true);
//     	$split_data = $data['cards'][0]['card_group'];
    	
    	$this->_ci->load->model('split/htmlsearchparser_model');
    	$split_data = $this->_ci->htmlsearchparser_model->parser_sina_phone_info($result);
    	
    	
        return $split_data;
    }

    /**
     * 尝试抓取
     * @param bool $retry 是否使用代理和重试功能
     * @return array|string 错误时返回error数组 正确时返回抓取到的字符穿
     */

    private function try_crawl($retry = FALSE, $preg_url) {
        //临时数组
        $temp_array = array();
        //定义抓取参数
        $parameters = $this->make_parameters($preg_url);
        if(is_array($parameters) && array_key_exists($this->_error_string, $parameters)) {
            log_message($this->_error_string, 'Crawl From Sina Wap Make CURL Parameters Error', FALSE, $this->_log_dir);
            return $parameters;
        }
        if($retry == FALSE) {
            //不使用代理、不重试直接抓取
            log_message(self::LOG_LEVEL_NOTICE, 'Will Crawl From Sina Wap Un Use Retry And Un Use Proxy With Parameters: '.json_encode($this->_parameters), FALSE, $this->_log_dir);
            return self::get_content($this->_parameters);
        } else {
            //$crawl_use_proxy_ip_result = $this->crawl_use_proxy_ip($this->_parameters, self::PRROXY_IP, $this->_crawl_from['sina_phone'], TRUE);
            //if(is_array($crawl_use_proxy_ip_result)) {
                //if(!array_key_exists($this->_error_string, $crawl_use_proxy_ip_result)) {
                    //$temp_array = $crawl_use_proxy_ip_result;
                //}

                $re_crawl_result = $this->crawl_use_proxy_ip($this->_parameters, self::MOBILITY_IP, $this->_crawl_from['sina_phone'], TRUE);

                if(is_array($re_crawl_result) && !array_key_exists($this->_error_string, $re_crawl_result) && !array_key_exists($this->_null_data_array_key, $re_crawl_result)) {
                    return $this->make_crawl_return_array($temp_array, $re_crawl_result);
                } else if(count($temp_array) > 0) {
                    return $this->make_crawl_return_array($temp_array);
                }else {
                    return $re_crawl_result;
                }
            //} else {
                //return $crawl_use_proxy_ip_result;
            //}
        }
    }

    /**
     * 抓取内容验证
     * @param $html
     * @return bool
     */
    public function match_html($html) {
    	return TRUE;
        if(strpos($html, $this->_htmlsearch_config['no_login'])) {
            
            //如果返回登陆页 重新组织参数 更新COOKIE
            $this->make_parameters();
            
            //$this->label_cookie($this->_cookie['id']);
            return $this->_ci->error_code_library->getError('curl', 'data', 'cookie_error', $this->_htmlsearch_config['no_login']);
        } elseif(strpos($html, $this->_htmlsearch_config['result_error'])) {
            $this->label_proxy_table($this->_proxy_ip, $this->_use_proxy);
            return $this->_ci->error_code_library->getError('curl', 'data', 'verification_code', $this->_htmlsearch_config['result_error']);
        } elseif(strpos($html, $this->_htmlsearch_config['result_success']) === FALSE) {
            return $this->_ci->error_code_library->getError('curl', 'data', 'incomplete_answers', $this->_htmlsearch_config['result_success']);
        } else {
            return TRUE;
        }

    }
    
        /**
         * 拼接请求URL
         * 判断传入的链接个数
         * 提取要拼接的参数
         * 
         *
         *
         */
    private function make_parameters($preg_url) {
 
        $parameters['url'] = 'https://m.weibo.cn/status/'.trim($preg_url);
        $parameters['time_out'] = 5;

        //若传入cookie信息.则使用传入的cookie信息,用于调试cookie 是否有效使用 add by hejigui
        $cookie = "_T_WM=41ac783fcaf57c19922d2062d71e1c77; ALF=1494729412; SCF=Ajz54eLCLVWpEN4Toq8CgwNToyKSwYZLb9uvSN13TG0uD9-cTGh086UcPth2AV4tez-A0GJ-kJtAf24gExIUR8E.; SUB=_2A2519EWaDeThGeRN6lEY8y_FwjmIHXVXF2vSrDV6PUJbktBeLWHnkW0gkWCvUN00fuOEC7MvXJeGIPAM6g..; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5Gu4wkakvVoO8xJmc9Rvf65JpX5o2p5NHD95QEe0201Kep1K.fWs4Dqcj.i--NiKyhi-8Wi--NiKyhi-8Wi--ci-2Ei-2Ri--fi-82iK.7; SUHB=0tK7PKNJlr5xq-; SSOLoginState=1492137419; M_WEIBOCN_PARAMS=featurecode%3D20000180%26oid%3D4084830210341878%26luicode%3D20000174%26lfid%3D2302835139152205%26fid%3D1076035139152205%26uicode%3D10000011";
    	
    	//$parameters['cookie_str'] = true;
    	//$parameters['cookie'] = $cookie;
    	
    	return $this->_parameters = $parameters;
    }

    /* private function make_parameters() {
    	//若传入cookie信息.则使用传入的cookie信息,用于调试cookie 是否有效使用 add by hejigui

        $parameters['url'] = "http://m.weibo.cn/page/json";
        $parameters['method'] = self::METHOD_GET;
        $parameters['postData']['containerid'] = urlencode('100505'.trim($this->_ci->input->post('uid')).'_-_WEIBO_SECOND_PROFILE_WEIBO');
        $parameters['postData']['page'] = $this->_ci->input->post('page'); // 上面的方法只能选择10页,所以直接从参数里拿.可以用到100页.

        //$parameters['cookie_str'] = true;
        //$parameters['cookie'] = $this->_cookie['wap_cookie'];
        //$this->update_cookie_wap_count($this->_cookie['id']);
        
        return $this->_parameters = $parameters;
    } */

}
